package cn.codeforfun.machine;

import cn.codeforfun.base.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @author wangbin
 */
@Service
public class MachineService extends BaseService<Machine, MachineRepository> {
    @Resource
    private MachineRepository machineRepository;

    public MachineService(MachineRepository repository) {
        super(repository);
    }

    @Override
    public void save(Machine machine) {
        String password = null;
        if (ObjectUtils.isEmpty(machine.getPassword())) {
            password = machineRepository.findById(machine.getId()).orElse(machine).getPassword();
        }
        machine.setPassword(password);
        super.save(machine);
    }

    public void changePassword(String id, String password) {
        Machine machine = machineRepository.findById(id).orElseThrow(NullPointerException::new);
        machine.setPassword(password);
        machineRepository.save(machine);
    }
}
